<%@ include file="../import_page.jsp"%>
<%
//login checking
	if(!loginedChecking(session, UsersType.student)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	//input parameter
	int acId = -1;
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	//Convert string to int
	try {
		acId = Integer.parseInt(acIdStr);
		if(acId < 1)
			throw new Exception();
	} catch(Exception err) {
		//incorrect input, go back view assignment
		response.sendRedirect(URL_SELECT_ASSIGNMENT);
		return;
	}
	
	//input parameter
	int amOrder = -1;
	String amOrderStr = request.getParameter(PAR_AM_ORDER);
	try {
		amOrder = Integer.parseInt(amOrderStr);
		if(amOrder < 1)
			throw new Exception();
	} catch(Exception err) {
		//incorrect input, go back view assignment
		response.sendRedirect(URL_SELECT_ASSIGNMENT);
		return;
	}
	
	String filename = "";
	String filePath = "";
	try {
		//Get file name & path from database
		AssignmentsControl aCon = new AssignmentsControl();
		filePath = aCon.getAmDownloadPath(acId, amOrder);
		filename = org.apache.commons.io.FilenameUtils.getName(filePath);
		aCon.closeDBConnection();
	} catch(Exception e) {
		out.println("On download_material");
		out.println(e.toString());
		return;
	}

	//follow to download_process
	RequestDispatcher dispatcher = request.getRequestDispatcher(URL_DOWNLOAD_PROCESS);
	request.setAttribute("filename", filename);
	request.setAttribute("filePath", filePath);
	dispatcher.forward(request, response);
	return;
%>